// pages/goods/search/index.js
var app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        parameter: {
            'navbar': '1',
            'return': '1',
            'title': '商品搜索',
            'color': true,
            'class': false
        },
        executorUserId: "",
        historyGoods: wx.getStorageSync('historyGoods') || [],
        store: null,
        inputValue: "",
        goodsList: [],
        showHistory: true,
        isSearchList: true,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        wx.getStorage({
            key: 'userInfo',
            success(res) {
                that.setData({
                    executorUserId: res.data.id,
                    historyGoods: wx.getStorageSync('historyGoods') || [],
                    status: options.status
                })
            }
        })
    },
    // 读取输入值
    inputText: function (e) {
        this.setData({
            inputValue: e.detail.value
        })
    },
    //把用户输入的值保存在inputValue中
    doSearch: function (e) {
        var inputValue = this.data.inputValue;
        var historyGoods = this.data.historyGoods || [];
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];
        if (inputValue) {
            historyGoods.push(inputValue);
            wx.setStorageSync('historyGoods', historyGoods);
        }
        prevPage.setData({
             value: inputValue,
             isNav: this.data.status
        })
        wx.navigateBack({
             delta: 1,
        })
    },
    //清空搜索历史
    clearHistory: function () {
        wx.removeStorageSync('historyGoods')
        this.setData({
            historyGoods: []
        })
    },
    // 返回上一页
    back: function(e) {
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];
        prevPage.setData({
             value: e.currentTarget.dataset.item,
             isNav: this.data.status
        })
        wx.navigateBack({
             delta: 1,
        })
   }
})